var POST = "POST";
var GET = "GET";

var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
var request = null;

function getXMLHTTPRequest() {
	var xRequest=null;
	if (window.XMLHttpRequest) {
		xRequest=new XMLHttpRequest();
	}else if (typeof ActiveXObject != "undefined"){
		xRequest=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xRequest;
}


function sendRequest(url,HttpMethod,params){
    if (!HttpMethod){
        HttpMethod=GET;
    }
    request = getXMLHTTPRequest();
    if (request){
        request.onreadystatechange=onReadyStateChange;
        if(HttpMethod == GET && params.length > 0){
        	url+="?" + params;
        }
        request.open(HttpMethod,url,true);
        if(HttpMethod == POST){
        	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        	request.send(params);
        }else {
        	request.send(null);
        }            
    }
}

function onReadyStateChange(){
    if (request.readyState == 4 && request.status == 200) {
    	document.getElementById("content").innerHTML = request.responseText;
    }
}

function deleteFromList() {
	var tags = document.getElementsByName("selectedNews");
	var result = false;
	var params = "method=delete";
	for ( var i = 0; i < tags.length; i++) {
		if (tags[i].checked) {			
			result = true;
			params += "&selectedNews=" + tags[i].value;
		}
	}
	if (!result) {
		alert(noSelectedNews);
	}else {
		var agree = confirm(confirmDeleteList);
		if(agree){
			sendRequest('newsAction.do', POST, params);
		}
	}
}
function deleteFromView() {
	var agree = confirm(confirmdDeleteNews);
	if(agree){
		sendRequest('newsAction.do', POST, 'method=delete');
	}
}
function saveNews() {
	var messages = new Array();
	var isValid = true;
	var brief;
	var title;
	var date;
	var content;
	var element = document.getElementById('add-news-title');
	title = element.value;
	if((title == '') || (title.length > 100)) {
		isValid =  false;
		messages.push(invalidTitle);
	}
	element = document.getElementById('add-news-date');
	date = element.value;
	if((date == '') || (date.length > 10)){
		isValid = false;
		messages.push(invalidDate+datePattern);
	}else {
		var MONTH = "MM";
		var DAY = "dd";
		var YEAR = "yyyy";
		var dateRegexp;
		var orderMonth = datePattern.indexOf(MONTH);
		var orderDay = datePattern.indexOf(DAY);
		var orderYear = datePattern.indexOf(YEAR);
		if ((orderDay < orderYear && orderDay > orderMonth)) {
			var iDelim1 = orderMonth + MONTH.length;
			var iDelim2 = orderDay + DAY.length;
			var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
			var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
			if (iDelim1 == orderDay && iDelim2 == orderYear) {
				dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$");
			} else if (iDelim1 == orderDay) {
				dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$");
			} else if (iDelim2 == orderYear) {
				dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$");
			} else {
				dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
			}
			var matched = dateRegexp.exec(element.value);
			if(matched != null) {
				if (!jcv_isValidDate(matched[2], matched[1], matched[3])) {
					isValid = false;
					messages.push(invalidDate+datePattern);
				}
			} else {
				isValid = false;
				messages.push(invalidDate+datePattern);
			}
		} else if ((orderMonth < orderYear && orderMonth > orderDay)) {
			var iDelim1 = orderDay + DAY.length;
			var iDelim2 = orderMonth + MONTH.length;
			var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
			var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
			if (iDelim1 == orderMonth && iDelim2 == orderYear) {
				dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$");
			} else if (iDelim1 == orderMonth) {
				dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$");
			} else if (iDelim2 == orderYear) {
				dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$");
			} else {
				dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
			}
			var matched = dateRegexp.exec(element.value);
			if(matched != null) {
				if (!jcv_isValidDate(matched[1], matched[2], matched[3])) {
					isValid = false;
					messages.push(invalidDate+datePattern);
				}
			} else {
				isValid = false;
				messages.push(invalidDate+datePattern);
			}
		} else if ((orderMonth > orderYear && orderMonth < orderDay)) {
			var iDelim1 = orderYear + YEAR.length;
			var iDelim2 = orderMonth + MONTH.length;
			var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
			var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
			if (iDelim1 == orderMonth && iDelim2 == orderDay) {
				dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})$");
			} else if (iDelim1 == orderMonth) {
				dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})$");
			} else if (iDelim2 == orderDay) {
				dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})$");
			} else {
				dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})$");
			}
			var matched = dateRegexp.exec(element.value);
			if(matched != null) {
				if (!jcv_isValidDate(matched[3], matched[2], matched[1])) {
					isValid = false;
					messages.push(invalidDate + datePattern);
				}
			} else {
				isValid = false;
				messages.push(invalidDate+datePattern);
			}
		} else {
			isValid = false;
			messages.push(invalidDate+datePattern);
		} 
	}
	
	element = document.getElementById('news-brief');
	brief = element.value;
	if((brief == '') || (brief.length > 500)) {
		isValid = false;
		messages.push(invalidBrief);
	}
	
	element = document.getElementById('news-content');
	content = element.value;
	if((content == '') || (content.length > 2048)){
		isValid = false;
		messages.push(invalidContent);
	}
	if(!isValid){
		var message = '';
		for(var i = 0; i < messages.length; i++){
			message += messages[i];
			message += '\n';
		}
		alert(message);
	} else {
		var params="method=save" + "&news.title=" + title +
					"&news.brief=" + brief + "&news.content="
					+ content +"&dateString=" + date;
		sendRequest('newsAction.do', POST, params);
	}	
}
function changeLocale(){
	alert("LOCALE");
	var element;
	element = document.getElementById("link-en");
	element.innerText = headerEnglish;
	
	element = document.getElementById("link-en");
	element.innerText = headerEnglish;
	
	element = document.getElementById("menu-link-list");
	element.innerText = menuList;
	
	element = document.getElementById("menu-link-add");
	element.innerText = menuAdd;	
}
function jcv_isValidDate(day, month, year) {
	if (month < 1 || month > 12) {
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month == 4 || month == 6 || month == 9 || month == 11) &&
			(day == 31)) {
		return false;
	}
	if (month == 2) {
		var leap = (year % 4 == 0 &&
				(year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day == 29 && !leap)) {
			return false;
		}
	}
	return true;
}

function ItpOverlay (id) {
	
	this.id = id;
	

	this.show = function(id){
	  if(id){
		  this.id = id;
	  }
	  

	  var bgObj = document.getElementById(this.id);
		
	
	  var oDiv = document.createElement('div');
	  oDiv.setAttribute('id','itp_overlay');
	  oDiv.setAttribute("class", "black_overlay");
	  oDiv.style.display='block';
	  bgObj.appendChild(oDiv);
	  
	 
	  var lDiv = document.createElement('div');
	  lDiv.setAttribute('id','loading');
	  lDiv.setAttribute("class", "loading");
	  lDiv.style.display='block';
	  bgObj.appendChild(lDiv);
	  
	};


	this.hide = function(id) {
		
		if(id){
			this.id = id;
		}
		
		var bgObj   = document.getElementById(this.id);
		
	
		var element = document.getElementById('loading');
		bgObj.removeChild(element);
		

		var element = document.getElementById('itp_overlay');
		bgObj.removeChild(element);
	};
}